<template>
  <div class="car-rental-container">
    <header class="header" >
      <el-menu   mode="horizontal" @select="handleSelect" class="center-menu" style="background:  #66b1ff;">
        <el-menu-item  :style="{ backgroundColor: '#66b1ff' }" style="color: white !important;font-size: 30px">汽车租赁</el-menu-item>
        <el-menu-item @click="openIndex('/shouye')"  :style="{ backgroundColor: '#66b1ff' }" class="item1" style="color: white !important;margin-left: 100px;">首页</el-menu-item>
        <el-menu-item @click="openIndex('/zuche')" :style="{ backgroundColor: '#66b1ff'} " style="color: white !important;">租车服务</el-menu-item>
        <el-menu-item @click="openIndex('/carList')" :style="{ backgroundColor: '#66b1ff' }" style="color: white !important;">车圈</el-menu-item>
        <el-menu-item @click="openIndex('/orders')" :style="{ backgroundColor: 'rgb(105,176,250)' }" style="color: white !important;">我的订单</el-menu-item>

        <div style="margin-left: 93%" >
          <el-dropdown>
            <!--                <el-avatar icon="el-icon-user-solid" style="margin-top: 10px"></el-avatar>-->
            <div class="avatar-wrapper">
              <img :src="require('@/assets/images/user.png')" style="width: 40px;height: 40px;margin-top: 10px">
              <i class="el-icon-caret-bottom" />
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="goToCommentsPage()">个人中心</el-dropdown-item>
              <el-dropdown-item divided @click.native="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-menu>
    </header>
    <div class="car-container">
    <!-- 搜索和过滤区域 -->
    <div class="filter-section">
      <input
        type="text"
        v-model="queryParams.vehicleName"
        placeholder="搜索车型"
        class="search-input"
      >
      <div class="filter-buttons">
          <el-select clearable v-model="queryParams.vehicleType" placeholder="请选择车辆类型">
            <el-option label="经济型" value="0">经济型</el-option>
            <el-option label="豪华型" value="1">豪华型</el-option>
          </el-select>
      </div>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
    </div>

    <!-- 汽车列表 -->
    <div class="car-list">
      <div
        v-for="car in cars"
        :key="car.vehicleId"
        class="car-card"
      >
        <div class="car-image">
          <img :src="baseURL+car.vehicleImageUrl" :alt="car.vehicleName">
        </div>
        <div class="car-details">
          <h3 class="car-name">{{ car.vehicleName }}</h3>
          <div class="car-info">
            <span class="car-type" v-if="car.vehicleType=='0'">经济型</span>
            <span class="car-type" v-if="car.vehicleType=='1'">豪华型</span>
            <span class="car-price">¥{{ car.vehiclePrice }}/日</span>
          </div>
        </div>
        <el-button
          type="primary"
          class="book-btn"
          @click="bookCar(car.vehicleId)"
        >立即预订
        </el-button>
      </div>
    </div>

    <!-- 分页组件 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <UserProfile />
    </el-dialog>

    <el-dialog :title="'预定信息'" :visible.sync="clopen" width="500px" append-to-body>
      <el-form ref="form" :model="form"  label-width="80px">
        <el-form-item label="车辆名称" prop="vehicleName">
          <el-input disabled v-model="form.vehicleName" placeholder="请输入车辆名称" />
        </el-form-item>
        <el-form-item label="租赁价格(/天)" prop="vehiclePrice" label-width="100px">
          <el-input disabled v-model="form.vehiclePrice" placeholder="请输入租赁价格" />
        </el-form-item>
        <el-form-item label="车辆类型" prop="vehicleType">
          <el-select disabled clearable v-model="form.vehicleType" placeholder="请选择车辆类型">
            <el-option label="经济型" value="0">经济型</el-option>
            <el-option label="豪华型" value="1">豪华型</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆图片" prop="vehicleImageUrl">
          <img :src="baseURL+form.vehicleImageUrl"  style="width: 150px;height: 150px;"  />
        </el-form-item>

        <el-form-item label="取车时间" prop="createdAt">
          <el-date-picker clearable
                          v-model="form.pickupDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择取车时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="租车天数" prop="createdAt" >
          <el-input  v-model="form.rentalDuration" @input="zuche()" type="text">
          </el-input>
        </el-form-item>
        <el-form-item label="租车总价" prop="createdAt">
          <el-input disabled v-model="form.totalAmount" type="text">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {getToken} from "@/utils/auth";
import  UserProfile  from '@/views/system/user/profile/yhindex'
import {getRentals, listRentals} from "@/api/system/rentals";
import {addOrders} from "@/api/system/orders";

export default {
  name: 'CarRentalList',
  components: {
    UserProfile
  },
  data() {
    return {
      form: {},
      searchQuery: '',
      selectedType: 'all',
      baseURL:process.env.VUE_APP_BASE_API,
      total: 0,
      currentPage: 1,
      pageSize: 10,
      open: false,
      clopen: false,
      title: '用户信息',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleName: null,
        vehicleType: null,
        vehiclePrice: null,
        vehicleStatus: null,
        vehicleImageUrl: null,
        createdAt: null,
        updatedAt: null
      },
      cars: [
        {
          id: 1,
          name: '大众朗逸 2023款',
          type: '经济型',
          price: 268,
          image: require('@/assets/car_info/ad.jpg'),
        },
        {
          id: 2,
          name: '丰田卡罗拉 2022款',
          type: '经济型',
          price: 299,
          image: require('@/assets/car_info/ad.jpg'),
        },
        {
          id: 3,
          name: '本田雅阁 2023款',
          type: '舒适型',
          price: 488,
          image: require('@/assets/car_info/ad.jpg'),
        },
        {
          id: 4,
          name: '本田雅阁 2023款',
          type: '舒适型',
          price: 488,
          image: require('@/assets/car_info/bm.jpg'),
        }
        // 可继续添加更多车辆数据...
      ]
    }
  },
  created() {
    if (!getToken()) {
      location.href = '/index'
    }
    this.getList();
  },
  methods: {
    /** 查询车辆租赁信息列表 */
    getList() {
      this.loading = true;
      listRentals(this.queryParams).then(response => {
        this.cars = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.vehicleName = '';
      this.queryParams.vehicleType = null;
      this.handleQuery();
    },
    handleSelect(key) {
      console.log(key);
    },
    zuche() {
      this.form.totalAmount = this.form.vehiclePrice * this.form.rentalDuration;
    },
    bookCar(carId) {
      this.reset();
      const vehicleId = carId
      getRentals(vehicleId).then(response => {
        this.form = response.data;
        this.clopen = true;
        this.title = "车辆信息";
      });
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleId: null,
        pickupDate: null,
        vehicleName: null,
        vehicleType: null,
        vehiclePrice: null,
        vehicleStatus: null,
        vehicleImageUrl: null,
        createdAt: null,
        totalAmount: null,
        rentalDuration: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    goToCommentsPage() {
      // 处理预订逻辑
      this.open=true
    },
    logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index'
        })
      }).catch(() => {})
    },
    toggleFilter(type) {
      this.selectedType = this.selectedType === type ? 'all' : type
    },
    handlePageChange(page) {
      this.currentPage = page;
    },
    openIndex(index) {
      // 处理预订逻辑
      location.href=index
    },
  submitForm(){
    if (!this.form.pickupDate){
      return this.$modal.msgError("请选择取车日期");
    }
    if (!this.form.rentalDuration){
      return this.$modal.msgError("请输入租车天数");
    }
    this.$refs["form"].validate(valid => {
      this.form.rentalPrice=this.form.vehiclePrice
      addOrders(this.form).then(response => {
        this.$modal.msgSuccess("预定成功");
        this.clopen = false;
        this.getList();
      });
    });

  },
  cancel(){
    this.clopen=false
  }

},

}
</script>

<style scoped>
.car-rental-container {
  max-width: 100%;
  margin: 0 auto;
}

.car-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.filter-section {
  display: flex;
  gap: 15px;
  margin-bottom: 30px;
}

.search-input {
  flex: 1;
  max-width: 400px;
  padding: 12px;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.search-input:focus {
  outline: none;
  border-color: #4299e1;
}

.filter-buttons {
  display: flex;
  gap: 10px;
}

.filter-btn {
  padding: 12px 24px;
  background: #fff;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.filter-btn.active {
  background: #4299e1;
  color: white;
  border-color: #4299e1;
}

.car-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.car-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s;
}

.car-card:hover {
  transform: translateY(-5px);
}

.car-image img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.car-details {
  padding: 20px;
}

.car-name {
  margin: 0 0 10px;
  color: #2d3748;
  font-size: 1.2rem;
}

.car-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}

.car-type {
  background: #c6f6d5;
  color: #22543d;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.9rem;
}

.car-price {
  color: #4299e1;
  font-weight: bold;
  font-size: 1.1rem;
}

.car-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  background: #edf2f7;
  color: #4a5568;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 0.85rem;
}

.no-results {
  text-align: center;
  padding: 40px;
  color: #718096;
  font-size: 1.2rem;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}

.header {
  background: #409EFF; /* 修改为浅蓝色 */
  box-shadow: 0 2px 12px rgba(189, 93, 93, 0.1);
  /*padding: 0 20px;*/
}
</style>
